Const kITEM_BOMB=0
Const kITEM_SPEED=1
Const kITEM_MISSIL=2

Type TItem
	Field m ; mesh
	Field cat; categorie 0=Bomb, 1=Speed Bonus, 2=missil 
	Field X#,Y#,Z# ; position in 3d-space
 	Field DisplayTimeLive.Timer
End Type 


Function TItem_CreateBomb.TItem( pX#, pY#, pZ#)
	Local l.TItem = New TItem
	l\m = CreateSphere(5)
	PositionEntity l\m, pX#, pY#, pZ#
	uCelShading( l\m,0.5,0.5,.5,0.05)
	EntityColor l\m, 84,84,84	
	
	
	l\cat = kITEM_BOMB
	l\DisplayTimeLive = SetTimer( Rand(4000, 6000) )
	Return l
End Function	

Function TItem_CreateSpeed.TItem( pX#, pY#, pZ#)
	Local l.TItem = New TItem
	l\m = CreatePillMesh(1,1.2,3)
	uCelShading( l\m,0.5,0.5,.5,0.05)
	PositionEntity l\m, pX#, pY#, pZ#
	EntityColor l\m, 255,255,0
	l\cat = kITEM_SPEED
	l\DisplayTimeLive = SetTimer( Rand(4000, 6000) )
	Return l
End Function	

Function TItem_CreateMissil.TItem( pX#, pY#, pZ#)
	Local l.TItem = New TItem
	l\m = CopyEntity(g_App\TEMPLATE_MISSIL) 
	PositionEntity l\m, pX#, pY#, pZ#
	l\cat = kITEM_MISSIL
	l\DisplayTimeLive = SetTimer( Rand(5000, 7000) )
	Return l
End Function	


Function isItemExistNearWayPoint( pWp.TWayPoint )
	Local i.TItem=Null
	For i.TItem = Each TItem
		If EntityDistance( pWp\mesh, i\m ) < 30
			Return True
		EndIf 
	Next 
	Return False 
End Function 


Function TItem_Generate(p_factor=50, p_randMax=1000)
	Local cat,wp_index
	Local wp.TWayPoint
	If Rand( 1, p_randMax) < p_factor Then
		;cat = Rand(kITEM_BOMB,kITEM_MISSIL)
		cat = Rand(kITEM_MISSIL,kITEM_MISSIL)
		wp_index=Rand(1,TWayPoint_Count() )
		wp.TWayPoint = TWayPoint_Get(wp_index)
		If isItemExistNearWayPoint( wp) Then Return 
		If cat=kITEM_BOMB Then TItem_CreateBomb(EntityX(wp\mesh)+Rand(-4,4), 1, EntityZ(wp\mesh)+Rand(-4,4))
		If cat=kITEM_SPEED Then TItem_CreateSpeed(EntityX(wp\mesh)+Rand(-4,4), 1, EntityZ(wp\mesh)+Rand(-4,4))
		If cat=kITEM_MISSIL Then TItem_CreateMissil(EntityX(wp\mesh)+Rand(-4,4), 1, EntityZ(wp\mesh)+Rand(-4,4))
	End If
End Function

Function TItem_GetNearest_TCar_InCollisonWith_TItem.TCar( p_I.TItem )
	Local l_c.TCar=Null
	For l_c = Each TCar
		If EntityDistance( p_I\m, l_c\mesh ) < 2 
			Return l_c
			Exit
		EndIf
	Next 
	Return Null
End Function


Function TItem_Update()
	TItem_Generate()
	
	Local c.TCar=Null
	Local i.TItem=Null
	
	; for each free item on road
	For i.TItem = Each TItem
		
		; if end of live then delete on road
		If TimeOut(i\DisplayTimeLive) = True 
			If EntityInView( i\m,g_App\Cam1)
				TExplosion_Create( i\m,1 )
			EndIf 	
			TItem_Free(i)
		Else
			; get the car in collision with the current item
			c=Null
			c=TItem_GetNearest_TCar_InCollisonWith_TItem( i )
			
			If c <> Null
				
				Select i\cat  
					; explode when contact with a bomb	
					Case kITEM_BOMB
						TExplosion_Create( i\m,1 )
						TExplosion_Create( c\mesh,1 )
						
						c\bExploded=True
						c\WaitExplodeTime=SetTimer(2000)
						
						c\OldX# = EntityX( c\mesh )
						c\OldY# = EntityY( c\mesh )
						c\OldZ# = EntityZ( c\mesh )
						
						HideEntity c\mesh 
					Case kITEM_MISSIL 
						TurnEntity i\m, 0,3,0
						
						If c\bMissil = False
							c\bMissil = True
						EndIf  
						
					Case kITEM_SPEED
						If c\bSpeed = False 
							c\bSpeed = True
							c\WaitSpeedTime=SetTimer(4000)
							c\oldspeed = c\speed
							c\speed#=c\speed#*2
						EndIf 
				End Select 
				TItem_Free( i )
			EndIf
		EndIf 
	Next 
	
End Function 


Function TItem_Free( p_I.TItem )
	FreeEntity(p_I\m)
	TimeOut(p_I\DisplayTimeLive)
	Delete p_I
End Function	


Function CreatePillMesh(rad# = 1, height# = 2, seg = 8)
	Local mesh = CreateCylinder(seg * 2)
	ScaleMesh mesh, rad#, height# * .6, rad#
	Local c = CreateSphere(seg)
	ScaleMesh c, rad#, rad#, rad#
	PositionMesh c, 0, height# * .5, 0
	AddMesh c, mesh
	FreeEntity c
	c = CreateSphere(seg)
	ScaleMesh c, rad#, rad#, rad#
	PositionMesh c, 0, -height# * .5, 0
	AddMesh c, mesh
	FreeEntity c
	Return mesh
End Function


Function TItem_Template_Missil( p_sFile$ )
	Local l_m= LoadMesh(p_sFile$)
	ScaleMesh l_m,.5,.5,.5
	MoveEntity l_m,0,1,0
	FitMesh l_m, -(MeshWidth (l_m) / 2), -(MeshHeight (l_m) / 2), -(MeshDepth (l_m) / 2), MeshWidth (l_m), MeshHeight (l_m), MeshDepth (l_m)
	EntityColor l_m, 0,125,255
	uCelShading( l_m )
	HideEntity l_m 
	Return l_m
End Function 

;~IDEal Editor Parameters:
;~B#6D#7B
;~C#Blitz3D